<?php

class Custom_Controller_Plugin_Folder extends Zend_Controller_Plugin_Abstract
{
	private static $_errormsgs = array();
	
	public function getSuffix($file,$dot=false)
	{
		if ($file===null) return false;
		
        $suffix= strtolower(pathinfo($file, PATHINFO_EXTENSION));
        if($dot){
			$suffix='.'.$suffix;
        }
		return $suffix;
	}
	
	/**
	 * 根据模式取到特定类型的文件
	 * @param $pattern
	 * @param $type
	 */
	public function getFile($pattern, $type = null)
	{
		if ($type === 'dir'){
		    return glob($pattern, GLOB_ONLYDIR);
		}elseif ($type === 'file'){
			$array = glob($pattern);
			return $array ? array_filter($array, 'is_file') : false;
		}else{
			return glob($pattern);
		}
	}
	
	/**
	 * 创建目录
	 * @param $path
	 * @param $mode
	 */
	public	function createDir($path, $mode = 0755)
	{
		if (is_dir($path)) return true;
		$dir = '';
		$path = $this->pathReplace($path);
		$dirlist = explode(DS, $path);
		array_pop($dirlist);
		foreach ($dirlist as $dirname){
			$dir .= $dirname.DS;
			if (!is_dir($dir)){
				if(@mkdir($dir, $mode)){
					@chmod($dir, $mode);
				}else{
					self::$_errormsgs[] = sprintf('Can not mkdir %s', $dir);
                    return false;
				}
			}
		}
		return true;
	}
	
	/**
	 * 删除目录内所有文件 $clear为True时,则只清空此文件夹下面数据,但不删除此文件夹
	 * @param $path
	 * @param $clear
	 */
	public function deleteFile($path,$clear=false)
	{
		if (!is_dir($path)) return false;
		$path = $this->pathReplace($path);
		$alldata = glob($path.'*');
		if (!is_array($alldata)) return true;
		foreach ($alldata as $data){
			if (is_dir($data)){
				$this->deleteFile($data,$clear);
			}else {
				if(!@unlink($data)){
					self::$_errormsgs[] = sprintf('can not delete file %s', $data);
                    return false;
				}
			}
		}
		if($clear===false){
			if(!@rmdir($path)){
				self::$_errormsgs[] = sprintf('can not rmdir %s', $path);
				return false;
			}
		}
		return true;
	}

	
	/**
	 * 移动数据文件,$copy为True地,表示复制数据文件
	 * @param unknown_type $source
	 * @param unknown_type $target
	 * @param unknown_type $copy
	 */
	public function moveFile($source, $target,$copy=false)
	{
		if (!is_dir($source)) return false;
		if (!is_dir($target)) $this->createDir($target);
		$source = $this->pathReplace($source);
		$target = $this->pathReplace($target);
		$alldata = glob($source.'*');
		if (!is_array($alldata)) return true;
		foreach ($alldata as $data){
			$basename = basename($data);
			$targetfile = $target.DS.$basename;
			if (is_dir($data)){
				$this->moveFile($data, $targetfile,$copy);
			}else {
				if(!@rename($data, $targetfile)){
					self::$_errormsgs[] = sprintf('can not move file %s to %s', $data, $targetfile);
                    return false;
				}
			}
		}
		if($copy===false){
			if(!@rmdir($source)) self::$_errormsgs[]=sprintf("can not rmdir $source");
		}
		return true;
	}
	

	/**
	 * 查找文件名或是文件内容的数据资料
	 * @param $path
	 * @param $pattern
	 * @param $mode
	 * @param $deep
	 */
	public function findFile($path, $pattern, $mode = 'filename', $deep = 0)
	{
		if (!is_dir($path)) return false;
		$path = $this->pathReplace($path);
		$alldata = glob($path.'*');
		if (!is_array($alldata)) return array();
		if ($mode == 'filename'){
			$finds = preg_grep($pattern, $alldata);
		}elseif ($mode == 'filedata'){
			foreach ($alldata as $file){
				if (is_file($file)) $data[$file] = file_get_contents($file);
			}
			$finds = array_keys(preg_grep($pattern, $data));
		}
		if ($deep){
			foreach ($alldata as $file){
				if (is_dir($file)) $finds = array_merge($finds, $this->findFile($file, $pattern, $mode, $deep));
			}
		}
		return $finds;
	}

	
	

	
	/**
	 * 取到所有文件和目录列表
	 * @param unknown_type $path
	 * @param unknown_type $type
	 * @param unknown_type $array
	 */
	public function listFile($path, $type = null, & $array = array())
	{
		if (!is_dir($path)) return false;
		$path = $this->pathReplace($path);
		$alldata = glob($path.'*');
		if (!is_array($alldata)) return $array;
		switch ($type) {
			case 'file':
				foreach ($alldata as $data){
					if (is_dir($data)){
						$this->listFile($data, $type, & $array);
					}else{
						$array[] = $data;
					}
				}
			break;
			
			case 'dir':
				foreach ($alldata as $data){
					if (is_dir($data)){
						$array[] = $data;
						$this->listFile($data, $type, & $array);
					}
				}
			break;
			
			default:
				foreach ($alldata as $data){
					if (is_dir($data)){
						$array['dir'][] = $data;
						$this->listFile($data, $type, & $array);
					}else{
						$array['file'][] = $data; 
					}
				}
			break;
		}
		
		return $array;
	}
	
	/**
	 * 读取文件大小
	 * @param $path
	 */
	public function sizeFile($path)
	{
		if (!is_dir($path)) return false;
		$size = 0;
		$path = $this->pathReplace($path);
		$alldata = glob($path.'*');
		if (!is_array($alldata)) return $size;
		foreach ($alldata as $data){
			if (is_dir($data)){
				$size += $this->sizeFile($data);
			}else{
				$size += filesize($data);
			}
		}
		return $size;
	}
	
	public function randMd5($value=null)
	{
		if($value===null){
			$value=md5(time().rand(2,1048576));
		}
		$dir='/'.substr($value,8,2).'/'.substr($value,23,2).'/'; 
		return $dir;
	}
	
	/**
	 * 转换文件大小
	 * @param unknown_type $filesize
	 */
	public function sizeUnit($filesize)
	{
		if($filesize >= 1073741824){
			$filesize = round($filesize / 1073741824 * 100) / 100 . ' GB';
		}elseif($filesize >= 1048576){
			$filesize = round($filesize / 1048576 * 100) / 100 . ' MB';
		}elseif($filesize >= 1024){
			$filesize = round($filesize / 1024 * 100) / 100 . ' KB';
		}else{
			$filesize = $filesize . ' Bytes';
		}
		return $filesize;
	}
	
	public function pathReplace($path)
	{
		return rtrim(preg_replace("|[\/]+|", DS, $path), DS).DS;
	}
	
	
	//产生随机目录
	public function randDir()
	{
		$key=rand(1,100);
		$dirlist='';
		$dirname=md5(time().$key);
		$dirlist = date('Y').'/'.date('m').'/'.date('d').'/'.substr($dirname,9,2).'/'.substr($dirname,27,2).'/';
		return $dirlist;
	}
	
	
	//重命名文件名
	public function getNewName($string){
		
		$suffix=$this->getSuffix($string,true);
		$string=md5($string.time());
    	return substr($string, 12,15).$suffix;
	
	}
	
	

}